package com.test.system.admin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.test.system.admin.util.HttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;

/**
 * @author 徒有琴
 */
@Controller
public class WeatherController {
    @RequestMapping("weather.html")
    public String page() {
        return "weather";
    }

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("search_weather.html")
    public String search(String city, Model model) {
       // try {
           // String url = "http://apis.juhe.cn/simpleWeather/query?key=48b29ee028de5abbd048983e37617814&city=" + URLEncoder.encode(city, "utf-8");
            String url = "http://apis.juhe.cn/simpleWeather/query?key=48b29ee028de5abbd048983e37617814&city="+city;
//            String result = HttpClient.doGet(url);
            HashMap json = restTemplate.getForObject(url, HashMap.class);
            System.out.println(json);
            //JSONObject json = JSON.parseObject(result);
            //model.addAttribute("result", result);
            model.addAttribute("json", json);
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
        return "weather";
    }
}
